#include "DXImage.h"
#include <d3dx9.h>
#include <vector>
#include "DX.h"

DXImage::DXImage( std::wstring fileURL, bool preload ) : _texture( 0 ), _fileURL( fileURL )
{
	if ( preload ) load();
}

DXImage::~DXImage( )
{
	if ( _texture ) {
		_texture->Release( );
	}
}

IDirect3DTexture9* DXImage::texture()
{
	if ( !_texture )
	{
		load();
	}
	return _texture;
}

void DXImage::load( )
{
	if ( D3DXCreateTextureFromFileEx(	DX::_()->device, 
												_fileURL.c_str(), 
												D3DX_DEFAULT_NONPOW2, 
												D3DX_DEFAULT_NONPOW2, 
												1, 0, D3DFMT_UNKNOWN, 
												D3DPOOL_DEFAULT, 
												D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, 0, 
												&_texture ) )
	{
		throw _fileURL + std::wstring( TEXT( "not loaded" ) );
	}
}

void DXImage::unload( )
{
	if ( _texture ) _texture->Release();
	_texture = 0;
}